SlideShare a Scribd company logo
1 of 37
Class No.21  Data Structures http://ecomputernotes.com
TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; //  k1 (first node in k2's left subtree)  //  will be the new root TreeNode<int>* k1 = k2->getLeft(); //  Y moves from k1's right to k2's left k2->setLeft( k1->getRight() );  k1->setRight(k2); //  reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); //  k2 is now k1's right subtree   h = Max( height(k1->getLeft()),  k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; //  k1 (first node in k2's left subtree)  //  will be the new root TreeNode<int>* k1 = k2->getLeft(); //  Y moves from k1's right to k2's left k2->setLeft( k1->getRight() );  k1->setRight(k2); //  reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); //  k2 is now k1's right subtree   h = Max( height(k1->getLeft()),  k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; //  k1 (first node in k2's left subtree)  //  will be the new root TreeNode<int>* k1 = k2->getLeft(); //  Y moves from k1's right to k2's left k2->setLeft( k1->getRight() );  k1->setRight(k2); //  reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); //  k2 is now k1's right subtree   h = Max( height(k1->getLeft()),  k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; //  k1 (first node in k2's left subtree)  //  will be the new root TreeNode<int>* k1 = k2->getLeft(); //  Y moves from k1's right to k2's left k2->setLeft( k1->getRight() );  k1->setRight(k2); //  reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); //  k2 is now k1's right subtree   h = Max( height(k1->getLeft()),  k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; //  k1 (first node in k2's left subtree)  //  will be the new root TreeNode<int>* k1 = k2->getLeft(); //  Y moves from k1's right to k2's left k2->setLeft( k1->getRight() );  k1->setRight(k2); //  reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); //  k2 is now k1's right subtree   h = Max( height(k1->getLeft()),  k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; //  k1 (first node in k2's left subtree)  //  will be the new root TreeNode<int>* k1 = k2->getLeft(); //  Y moves from k1's right to k2's left k2->setLeft( k1->getRight() );  k1->setRight(k2); //  reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); //  k2 is now k1's right subtree   h = Max( height(k1->getLeft()),  k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; //  k1 (first node in k2's left subtree)  //  will be the new root TreeNode<int>* k1 = k2->getLeft(); //  Y moves from k1's right to k2's left k2->setLeft( k1->getRight() );  k1->setRight(k2); //  reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); //  k2 is now k1's right subtree   h = Max( height(k1->getLeft()),  k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
int height( TreeNode<int>* node ) { if( node != NULL ) return node->getHeight(); return -1; }  height http://ecomputernotes.com
int height( TreeNode<int>* node ) { if( node != NULL ) return node->getHeight(); return -1; }  height http://ecomputernotes.com
TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; //  k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 );  //  reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); //  k1 is now k2's left subtree   h = Max( height(k2->getRight()),  k1->getHeight()); k2->setHeight( h+1 ); return k2;  }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; //  k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 );  //  reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); //  k1 is now k2's left subtree   h = Max( height(k2->getRight()),  k1->getHeight()); k2->setHeight( h+1 ); return k2;  }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; //  k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 );  //  reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); //  k1 is now k2's left subtree   h = Max( height(k2->getRight()),  k1->getHeight()); k2->setHeight( h+1 ); return k2;  }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; //  k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 );  //  reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); //  k1 is now k2's left subtree   h = Max( height(k2->getRight()),  k1->getHeight()); k2->setHeight( h+1 ); return k2;  }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; //  k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 );  //  reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); //  k1 is now k2's left subtree   h = Max( height(k2->getRight()),  k1->getHeight()); k2->setHeight( h+1 ); return k2;  }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; //  k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 );  //  reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); //  k1 is now k2's left subtree   h = Max( height(k2->getRight()),  k1->getHeight()); k2->setHeight( h+1 ); return k2;  }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
TreeNode<int>* doubleRightLeftRotation(TreeNode<int>* k1) { if( k1 == NULL ) return NULL; //  single right rotate with k3 (k1's right child) k1->setRight( singleRightRotation(k1->getRight())); //  now single left rotate with k1 as the root return singleLeftRotation(k1); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
TreeNode<int>* doubleRightLeftRotation(TreeNode<int>* k1) { if( k1 == NULL ) return NULL; //  single right rotate with k3 (k1's right child) k1->setRight( singleRightRotation(k1->getRight())); //  now single left rotate with k1 as the root return singleLeftRotation(k1); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
TreeNode<int>* doubleRightLeftRotation(TreeNode<int>* k1) { if( k1 == NULL ) return NULL; //  single right rotate with k3 (k1's right child) k1->setRight( singleRightRotation(k1->getRight())); //  now single left rotate with k1 as the root return singleLeftRotation(k1); }  k 1 k 2 D A B C k 3 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
TreeNode<int>* doubleLeftRightRotation(TreeNode<int>* k3) { if( k3 == NULL ) return NULL; //  single left rotate with k1 (k3's left child) k3->setLeft( singleLeftRotation(k3->getLeft())); //  now single right rotate with k3 as the root return singleRightRotation(k3); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
TreeNode<int>* doubleLeftRightRotation(TreeNode<int>* k3) { if( k3 == NULL ) return NULL; //  single left rotate with k1 (k3's left child) k3->setLeft( singleLeftRotation(k3->getLeft())); //  now single right rotate with k3 as the root return singleRightRotation(k3); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
TreeNode<int>* doubleLeftRightRotation(TreeNode<int>* k3) { if( k3 == NULL ) return NULL; //  single left rotate with k1 (k3's left child) k3->setLeft( singleLeftRotation(k3->getLeft())); //  now single right rotate with k3 as the root return singleRightRotation(k3); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
Deletion in AVL Tree ,[object Object],[object Object],http://ecomputernotes.com
Deletion in AVL Tree ,[object Object],[object Object],[object Object],http://ecomputernotes.com
Deletion in AVL Tree ,[object Object],A C D N E J G I F H K L M http://ecomputernotes.com
Deletion in AVL Tree ,[object Object],A C D N E J G I F H K L M http://ecomputernotes.com
Deletion in AVL Tree ,[object Object],C D N E J G I F H K L M http://ecomputernotes.com
Deletion in AVL Tree ,[object Object],C D N E J G I F H K L M http://ecomputernotes.com
Deletion in AVL Tree ,[object Object],C D N E J G I F H K L M http://ecomputernotes.com
Deletion in AVL Tree ,[object Object],[object Object],C D N E J G I F H K L M http://ecomputernotes.com
Deletion in AVL Tree ,[object Object],[object Object],[object Object],[object Object],[object Object],http://ecomputernotes.com
Deletion in AVL Tree ,[object Object],[object Object],[object Object],http://ecomputernotes.com
Deletion in AVL Tree Case 1a : the parent of the deleted node had a balance of 0 and the node was deleted in the parent’s  left  subtree. Action : change the balance of the parent node and stop. No further effect on balance of any higher node. Delete on this side http://ecomputernotes.com
Deletion in AVL Tree Here is why; the height of left tree does not change. 1 2 3 4 5 6 7 0 1 2
Deletion in AVL Tree Here is why; the height of left tree does not change. 1 2 3 4 5 6 7 2 3 4 5 6 7 0 1 2 remove(1)
Deletion in AVL Tree Case 1b : the parent of the deleted node had a balance of 0 and the node was deleted in the parent’s  right  subtree. Action : (same as  1a )   change the balance of the parent node and stop. No further effect on balance of any higher node. Delete on this side
Deletion in AVL Tree Case 2a : the parent of the deleted node had a balance of 1 and the node was deleted in the parent’s  left  subtree. Action : change the balance of the parent node. May have caused imbalance in higher nodes so continue up the tree. Delete on this side

More Related Content

What's hot

Powering code reuse with context and render props
Powering code reuse with context and render propsPowering code reuse with context and render props
Powering code reuse with context and render propsForbes Lindesay
 
Earth Engine on Google Cloud Platform (GCP)
Earth Engine on Google Cloud Platform (GCP)Earth Engine on Google Cloud Platform (GCP)
Earth Engine on Google Cloud Platform (GCP)Runcy Oommen
 
Better than you think: Handling JSON data in ClickHouse
Better than you think: Handling JSON data in ClickHouseBetter than you think: Handling JSON data in ClickHouse
Better than you think: Handling JSON data in ClickHouseAltinity Ltd
 
Appendix of downlink coverage probability in heterogeneous cellular networks ...
Appendix of downlink coverage probability in heterogeneous cellular networks ...Appendix of downlink coverage probability in heterogeneous cellular networks ...
Appendix of downlink coverage probability in heterogeneous cellular networks ...Cora Li
 
AJUG April 2011 Cascading example
AJUG April 2011 Cascading exampleAJUG April 2011 Cascading example
AJUG April 2011 Cascading exampleChristopher Curtin
 
Appendix of heterogeneous cellular network user distribution model
Appendix of heterogeneous cellular network user distribution modelAppendix of heterogeneous cellular network user distribution model
Appendix of heterogeneous cellular network user distribution modelCora Li
 
Clustering your Application with Hazelcast
Clustering your Application with HazelcastClustering your Application with Hazelcast
Clustering your Application with HazelcastHazelcast
 
K means clustering
K means clusteringK means clustering
K means clusteringKuppusamy P
 
Application of Google Earth Engine in Open NAPs
Application of Google Earth Engine in Open NAPsApplication of Google Earth Engine in Open NAPs
Application of Google Earth Engine in Open NAPsNAP Events
 
Discrete control2 converted
Discrete control2 convertedDiscrete control2 converted
Discrete control2 convertedcairo university
 
A Note on PCVB0 for HDP-LDA
A Note on PCVB0 for HDP-LDAA Note on PCVB0 for HDP-LDA
A Note on PCVB0 for HDP-LDATomonari Masada
 
Pyclustering tutorial - BANG
Pyclustering tutorial - BANGPyclustering tutorial - BANG
Pyclustering tutorial - BANGAndrei Novikov
 
Guaranteeing Consensus in Distriubuted Systems with CRDTs
Guaranteeing Consensus in Distriubuted Systems with CRDTsGuaranteeing Consensus in Distriubuted Systems with CRDTs
Guaranteeing Consensus in Distriubuted Systems with CRDTsSun-Li Beatteay
 
The Ring programming language version 1.3 book - Part 43 of 88
The Ring programming language version 1.3 book - Part 43 of 88The Ring programming language version 1.3 book - Part 43 of 88
The Ring programming language version 1.3 book - Part 43 of 88Mahmoud Samir Fayed
 
java experiments and programs
java experiments and programsjava experiments and programs
java experiments and programsKaruppaiyaa123
 
BMC Keynote @ 2016 Hadoop Summit
BMC Keynote @ 2016 Hadoop Summit BMC Keynote @ 2016 Hadoop Summit
BMC Keynote @ 2016 Hadoop Summit Jennifer Stern
 
Project Term2 Computer barun.docx
Project Term2 Computer barun.docxProject Term2 Computer barun.docx
Project Term2 Computer barun.docxSakshamSharma382989
 
Scaling up data science applications
Scaling up data science applicationsScaling up data science applications
Scaling up data science applicationsKexin Xie
 
Elastic response pseudo spectrum in c programming
Elastic response pseudo spectrum in c programmingElastic response pseudo spectrum in c programming
Elastic response pseudo spectrum in c programmingSalar Delavar Qashqai
 

What's hot (20)

Powering code reuse with context and render props
Powering code reuse with context and render propsPowering code reuse with context and render props
Powering code reuse with context and render props
 
Earth Engine on Google Cloud Platform (GCP)
Earth Engine on Google Cloud Platform (GCP)Earth Engine on Google Cloud Platform (GCP)
Earth Engine on Google Cloud Platform (GCP)
 
Better than you think: Handling JSON data in ClickHouse
Better than you think: Handling JSON data in ClickHouseBetter than you think: Handling JSON data in ClickHouse
Better than you think: Handling JSON data in ClickHouse
 
Appendix of downlink coverage probability in heterogeneous cellular networks ...
Appendix of downlink coverage probability in heterogeneous cellular networks ...Appendix of downlink coverage probability in heterogeneous cellular networks ...
Appendix of downlink coverage probability in heterogeneous cellular networks ...
 
AJUG April 2011 Cascading example
AJUG April 2011 Cascading exampleAJUG April 2011 Cascading example
AJUG April 2011 Cascading example
 
Appendix of heterogeneous cellular network user distribution model
Appendix of heterogeneous cellular network user distribution modelAppendix of heterogeneous cellular network user distribution model
Appendix of heterogeneous cellular network user distribution model
 
Clustering your Application with Hazelcast
Clustering your Application with HazelcastClustering your Application with Hazelcast
Clustering your Application with Hazelcast
 
K means clustering
K means clusteringK means clustering
K means clustering
 
Application of Google Earth Engine in Open NAPs
Application of Google Earth Engine in Open NAPsApplication of Google Earth Engine in Open NAPs
Application of Google Earth Engine in Open NAPs
 
Discrete control2 converted
Discrete control2 convertedDiscrete control2 converted
Discrete control2 converted
 
A Note on PCVB0 for HDP-LDA
A Note on PCVB0 for HDP-LDAA Note on PCVB0 for HDP-LDA
A Note on PCVB0 for HDP-LDA
 
Pyclustering tutorial - BANG
Pyclustering tutorial - BANGPyclustering tutorial - BANG
Pyclustering tutorial - BANG
 
Guaranteeing Consensus in Distriubuted Systems with CRDTs
Guaranteeing Consensus in Distriubuted Systems with CRDTsGuaranteeing Consensus in Distriubuted Systems with CRDTs
Guaranteeing Consensus in Distriubuted Systems with CRDTs
 
The Ring programming language version 1.3 book - Part 43 of 88
The Ring programming language version 1.3 book - Part 43 of 88The Ring programming language version 1.3 book - Part 43 of 88
The Ring programming language version 1.3 book - Part 43 of 88
 
java experiments and programs
java experiments and programsjava experiments and programs
java experiments and programs
 
BMC Keynote @ 2016 Hadoop Summit
BMC Keynote @ 2016 Hadoop Summit BMC Keynote @ 2016 Hadoop Summit
BMC Keynote @ 2016 Hadoop Summit
 
Project Term2 Computer barun.docx
Project Term2 Computer barun.docxProject Term2 Computer barun.docx
Project Term2 Computer barun.docx
 
Scaling up data science applications
Scaling up data science applicationsScaling up data science applications
Scaling up data science applications
 
Control assignment#4
Control assignment#4Control assignment#4
Control assignment#4
 
Elastic response pseudo spectrum in c programming
Elastic response pseudo spectrum in c programmingElastic response pseudo spectrum in c programming
Elastic response pseudo spectrum in c programming
 

Viewers also liked

computer notes - Data Structures - 38
computer notes - Data Structures - 38computer notes - Data Structures - 38
computer notes - Data Structures - 38ecomputernotes
 
computer notes - Data Structures - 5
computer notes - Data Structures - 5computer notes - Data Structures - 5
computer notes - Data Structures - 5ecomputernotes
 
computer notes - Data Structures - 12
computer notes - Data Structures - 12computer notes - Data Structures - 12
computer notes - Data Structures - 12ecomputernotes
 
computer notes - Data Structures - 14
computer notes - Data Structures - 14computer notes - Data Structures - 14
computer notes - Data Structures - 14ecomputernotes
 
computer notes - Data Structures - 4
computer notes - Data Structures - 4computer notes - Data Structures - 4
computer notes - Data Structures - 4ecomputernotes
 
computer notes - Data Structures - 20
computer notes - Data Structures - 20computer notes - Data Structures - 20
computer notes - Data Structures - 20ecomputernotes
 
computer notes - Data Structures - 18
computer notes - Data Structures - 18computer notes - Data Structures - 18
computer notes - Data Structures - 18ecomputernotes
 
computer notes - Data Structures - 1
computer notes - Data Structures - 1computer notes - Data Structures - 1
computer notes - Data Structures - 1ecomputernotes
 
computer notes - Data Structures - 28
computer notes - Data Structures - 28computer notes - Data Structures - 28
computer notes - Data Structures - 28ecomputernotes
 
computer notes - Data Structures - 27
computer notes - Data Structures - 27computer notes - Data Structures - 27
computer notes - Data Structures - 27ecomputernotes
 
computer notes - Data Structures - 33
computer notes - Data Structures - 33computer notes - Data Structures - 33
computer notes - Data Structures - 33ecomputernotes
 
computer notes - Data Structures - 19
computer notes - Data Structures - 19computer notes - Data Structures - 19
computer notes - Data Structures - 19ecomputernotes
 
computer notes - Data Structures - 39
computer notes - Data Structures - 39computer notes - Data Structures - 39
computer notes - Data Structures - 39ecomputernotes
 
computer notes - Data Structures - 3
computer notes - Data Structures - 3computer notes - Data Structures - 3
computer notes - Data Structures - 3ecomputernotes
 
computer notes - Data Structures - 26
computer notes - Data Structures - 26computer notes - Data Structures - 26
computer notes - Data Structures - 26ecomputernotes
 
computer notes - Data Structures - 29
computer notes - Data Structures - 29computer notes - Data Structures - 29
computer notes - Data Structures - 29ecomputernotes
 
computer notes - Data Structures - 8
computer notes - Data Structures - 8computer notes - Data Structures - 8
computer notes - Data Structures - 8ecomputernotes
 
computer notes - Data Structures - 31
computer notes - Data Structures - 31computer notes - Data Structures - 31
computer notes - Data Structures - 31ecomputernotes
 
computer notes - Data Structures - 9
computer notes - Data Structures - 9computer notes - Data Structures - 9
computer notes - Data Structures - 9ecomputernotes
 
computer notes - Data Structures - 7
computer notes - Data Structures - 7computer notes - Data Structures - 7
computer notes - Data Structures - 7ecomputernotes
 

Viewers also liked (20)

computer notes - Data Structures - 38
computer notes - Data Structures - 38computer notes - Data Structures - 38
computer notes - Data Structures - 38
 
computer notes - Data Structures - 5
computer notes - Data Structures - 5computer notes - Data Structures - 5
computer notes - Data Structures - 5
 
computer notes - Data Structures - 12
computer notes - Data Structures - 12computer notes - Data Structures - 12
computer notes - Data Structures - 12
 
computer notes - Data Structures - 14
computer notes - Data Structures - 14computer notes - Data Structures - 14
computer notes - Data Structures - 14
 
computer notes - Data Structures - 4
computer notes - Data Structures - 4computer notes - Data Structures - 4
computer notes - Data Structures - 4
 
computer notes - Data Structures - 20
computer notes - Data Structures - 20computer notes - Data Structures - 20
computer notes - Data Structures - 20
 
computer notes - Data Structures - 18
computer notes - Data Structures - 18computer notes - Data Structures - 18
computer notes - Data Structures - 18
 
computer notes - Data Structures - 1
computer notes - Data Structures - 1computer notes - Data Structures - 1
computer notes - Data Structures - 1
 
computer notes - Data Structures - 28
computer notes - Data Structures - 28computer notes - Data Structures - 28
computer notes - Data Structures - 28
 
computer notes - Data Structures - 27
computer notes - Data Structures - 27computer notes - Data Structures - 27
computer notes - Data Structures - 27
 
computer notes - Data Structures - 33
computer notes - Data Structures - 33computer notes - Data Structures - 33
computer notes - Data Structures - 33
 
computer notes - Data Structures - 19
computer notes - Data Structures - 19computer notes - Data Structures - 19
computer notes - Data Structures - 19
 
computer notes - Data Structures - 39
computer notes - Data Structures - 39computer notes - Data Structures - 39
computer notes - Data Structures - 39
 
computer notes - Data Structures - 3
computer notes - Data Structures - 3computer notes - Data Structures - 3
computer notes - Data Structures - 3
 
computer notes - Data Structures - 26
computer notes - Data Structures - 26computer notes - Data Structures - 26
computer notes - Data Structures - 26
 
computer notes - Data Structures - 29
computer notes - Data Structures - 29computer notes - Data Structures - 29
computer notes - Data Structures - 29
 
computer notes - Data Structures - 8
computer notes - Data Structures - 8computer notes - Data Structures - 8
computer notes - Data Structures - 8
 
computer notes - Data Structures - 31
computer notes - Data Structures - 31computer notes - Data Structures - 31
computer notes - Data Structures - 31
 
computer notes - Data Structures - 9
computer notes - Data Structures - 9computer notes - Data Structures - 9
computer notes - Data Structures - 9
 
computer notes - Data Structures - 7
computer notes - Data Structures - 7computer notes - Data Structures - 7
computer notes - Data Structures - 7
 

More from ecomputernotes

computer notes - Data Structures - 30
computer notes - Data Structures - 30computer notes - Data Structures - 30
computer notes - Data Structures - 30ecomputernotes
 
computer notes - Data Structures - 11
computer notes - Data Structures - 11computer notes - Data Structures - 11
computer notes - Data Structures - 11ecomputernotes
 
computer notes - Data Structures - 15
computer notes - Data Structures - 15computer notes - Data Structures - 15
computer notes - Data Structures - 15ecomputernotes
 
Computer notes - Including Constraints
Computer notes - Including ConstraintsComputer notes - Including Constraints
Computer notes - Including Constraintsecomputernotes
 
Computer notes - Date time Functions
Computer notes - Date time FunctionsComputer notes - Date time Functions
Computer notes - Date time Functionsecomputernotes
 
Computer notes - Subqueries
Computer notes - SubqueriesComputer notes - Subqueries
Computer notes - Subqueriesecomputernotes
 
Computer notes - Other Database Objects
Computer notes - Other Database ObjectsComputer notes - Other Database Objects
Computer notes - Other Database Objectsecomputernotes
 
computer notes - Data Structures - 13
computer notes - Data Structures - 13computer notes - Data Structures - 13
computer notes - Data Structures - 13ecomputernotes
 
Computer notes - Advanced Subqueries
Computer notes -   Advanced SubqueriesComputer notes -   Advanced Subqueries
Computer notes - Advanced Subqueriesecomputernotes
 
Computer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group FunctionsComputer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group Functionsecomputernotes
 
computer notes - Data Structures - 16
computer notes - Data Structures - 16computer notes - Data Structures - 16
computer notes - Data Structures - 16ecomputernotes
 
computer notes - Data Structures - 22
computer notes - Data Structures - 22computer notes - Data Structures - 22
computer notes - Data Structures - 22ecomputernotes
 
computer notes - Data Structures - 35
computer notes - Data Structures - 35computer notes - Data Structures - 35
computer notes - Data Structures - 35ecomputernotes
 
computer notes - Data Structures - 36
computer notes - Data Structures - 36computer notes - Data Structures - 36
computer notes - Data Structures - 36ecomputernotes
 
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Enhancements to the GROUP BY ClauseComputer notes - Enhancements to the GROUP BY Clause
Computer notes - Enhancements to the GROUP BY Clauseecomputernotes
 
Computer notes - Manipulating Data
Computer notes - Manipulating DataComputer notes - Manipulating Data
Computer notes - Manipulating Dataecomputernotes
 
Computer notes - Writing Basic SQL SELECT Statements
Computer notes - Writing Basic SQL SELECT StatementsComputer notes - Writing Basic SQL SELECT Statements
Computer notes - Writing Basic SQL SELECT Statementsecomputernotes
 
computer notes - Data Structures - 10
computer notes - Data Structures - 10computer notes - Data Structures - 10
computer notes - Data Structures - 10ecomputernotes
 
Computer notes - Controlling User Access
Computer notes - Controlling User AccessComputer notes - Controlling User Access
Computer notes - Controlling User Accessecomputernotes
 
Computer notes - Using SET Operator
Computer notes - Using SET OperatorComputer notes - Using SET Operator
Computer notes - Using SET Operatorecomputernotes
 

More from ecomputernotes (20)

computer notes - Data Structures - 30
computer notes - Data Structures - 30computer notes - Data Structures - 30
computer notes - Data Structures - 30
 
computer notes - Data Structures - 11
computer notes - Data Structures - 11computer notes - Data Structures - 11
computer notes - Data Structures - 11
 
computer notes - Data Structures - 15
computer notes - Data Structures - 15computer notes - Data Structures - 15
computer notes - Data Structures - 15
 
Computer notes - Including Constraints
Computer notes - Including ConstraintsComputer notes - Including Constraints
Computer notes - Including Constraints
 
Computer notes - Date time Functions
Computer notes - Date time FunctionsComputer notes - Date time Functions
Computer notes - Date time Functions
 
Computer notes - Subqueries
Computer notes - SubqueriesComputer notes - Subqueries
Computer notes - Subqueries
 
Computer notes - Other Database Objects
Computer notes - Other Database ObjectsComputer notes - Other Database Objects
Computer notes - Other Database Objects
 
computer notes - Data Structures - 13
computer notes - Data Structures - 13computer notes - Data Structures - 13
computer notes - Data Structures - 13
 
Computer notes - Advanced Subqueries
Computer notes -   Advanced SubqueriesComputer notes -   Advanced Subqueries
Computer notes - Advanced Subqueries
 
Computer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group FunctionsComputer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group Functions
 
computer notes - Data Structures - 16
computer notes - Data Structures - 16computer notes - Data Structures - 16
computer notes - Data Structures - 16
 
computer notes - Data Structures - 22
computer notes - Data Structures - 22computer notes - Data Structures - 22
computer notes - Data Structures - 22
 
computer notes - Data Structures - 35
computer notes - Data Structures - 35computer notes - Data Structures - 35
computer notes - Data Structures - 35
 
computer notes - Data Structures - 36
computer notes - Data Structures - 36computer notes - Data Structures - 36
computer notes - Data Structures - 36
 
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Enhancements to the GROUP BY ClauseComputer notes - Enhancements to the GROUP BY Clause
Computer notes - Enhancements to the GROUP BY Clause
 
Computer notes - Manipulating Data
Computer notes - Manipulating DataComputer notes - Manipulating Data
Computer notes - Manipulating Data
 
Computer notes - Writing Basic SQL SELECT Statements
Computer notes - Writing Basic SQL SELECT StatementsComputer notes - Writing Basic SQL SELECT Statements
Computer notes - Writing Basic SQL SELECT Statements
 
computer notes - Data Structures - 10
computer notes - Data Structures - 10computer notes - Data Structures - 10
computer notes - Data Structures - 10
 
Computer notes - Controlling User Access
Computer notes - Controlling User AccessComputer notes - Controlling User Access
Computer notes - Controlling User Access
 
Computer notes - Using SET Operator
Computer notes - Using SET OperatorComputer notes - Using SET Operator
Computer notes - Using SET Operator
 

Recently uploaded

Intro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxIntro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxFIDO Alliance
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard37
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024Lorenzo Miniero
 
Portal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russePortal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russe中 央社
 
Microsoft BitLocker Bypass Attack Method.pdf
Microsoft BitLocker Bypass Attack Method.pdfMicrosoft BitLocker Bypass Attack Method.pdf
Microsoft BitLocker Bypass Attack Method.pdfOverkill Security
 
UiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overviewUiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overviewDianaGray10
 
Generative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdfGenerative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdfalexjohnson7307
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxFIDO Alliance
 
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Skynet Technologies
 
Introduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptxIntroduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptxFIDO Alliance
 
State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!Memoori
 
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)Paige Cruz
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)Samir Dash
 
Event-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingEvent-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingScyllaDB
 
Microsoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireMicrosoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireExakis Nelite
 
Top 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTop 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTopCSSGallery
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfSrushith Repakula
 
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdfFrisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdfAnubhavMangla3
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
AI mind or machine power point presentation
AI mind or machine power point presentationAI mind or machine power point presentation
AI mind or machine power point presentationyogeshlabana357357
 

Recently uploaded (20)

Intro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxIntro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptx
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024
 
Portal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russePortal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russe
 
Microsoft BitLocker Bypass Attack Method.pdf
Microsoft BitLocker Bypass Attack Method.pdfMicrosoft BitLocker Bypass Attack Method.pdf
Microsoft BitLocker Bypass Attack Method.pdf
 
UiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overviewUiPath manufacturing technology benefits and AI overview
UiPath manufacturing technology benefits and AI overview
 
Generative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdfGenerative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdf
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptx
 
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
 
Introduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptxIntroduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptx
 
State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!
 
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
Event-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingEvent-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream Processing
 
Microsoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireMicrosoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - Questionnaire
 
Top 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTop 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development Companies
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdf
 
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdfFrisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
Frisco Automating Purchase Orders with MuleSoft IDP- May 10th, 2024.pptx.pdf
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
AI mind or machine power point presentation
AI mind or machine power point presentationAI mind or machine power point presentation
AI mind or machine power point presentation
 

computer notes - Data Structures - 21

  • 1. Class No.21 Data Structures http://ecomputernotes.com
  • 2. TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; // k1 (first node in k2's left subtree) // will be the new root TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); k1->setRight(k2); // reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); // k2 is now k1's right subtree h = Max( height(k1->getLeft()), k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
  • 3. TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; // k1 (first node in k2's left subtree) // will be the new root TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); k1->setRight(k2); // reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); // k2 is now k1's right subtree h = Max( height(k1->getLeft()), k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
  • 4. TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; // k1 (first node in k2's left subtree) // will be the new root TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); k1->setRight(k2); // reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); // k2 is now k1's right subtree h = Max( height(k1->getLeft()), k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
  • 5. TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; // k1 (first node in k2's left subtree) // will be the new root TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); k1->setRight(k2); // reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); // k2 is now k1's right subtree h = Max( height(k1->getLeft()), k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
  • 6. TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; // k1 (first node in k2's left subtree) // will be the new root TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); k1->setRight(k2); // reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); // k2 is now k1's right subtree h = Max( height(k1->getLeft()), k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
  • 7. TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; // k1 (first node in k2's left subtree) // will be the new root TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); k1->setRight(k2); // reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); // k2 is now k1's right subtree h = Max( height(k1->getLeft()), k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
  • 8. TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; // k1 (first node in k2's left subtree) // will be the new root TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); k1->setRight(k2); // reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 ); // k2 is now k1's right subtree h = Max( height(k1->getLeft()), k2->getHeight()); k1->setHeight( h+1 ); return k1; }  k 1 k 2 Z Y X k 1 k 2 Z Y X singleRightRotation http://ecomputernotes.com
  • 9. int height( TreeNode<int>* node ) { if( node != NULL ) return node->getHeight(); return -1; }  height http://ecomputernotes.com
  • 10. int height( TreeNode<int>* node ) { if( node != NULL ) return node->getHeight(); return -1; }  height http://ecomputernotes.com
  • 11. TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 ); // reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); // k1 is now k2's left subtree h = Max( height(k2->getRight()), k1->getHeight()); k2->setHeight( h+1 ); return k2; }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
  • 12. TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 ); // reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); // k1 is now k2's left subtree h = Max( height(k2->getRight()), k1->getHeight()); k2->setHeight( h+1 ); return k2; }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
  • 13. TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 ); // reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); // k1 is now k2's left subtree h = Max( height(k2->getRight()), k1->getHeight()); k2->setHeight( h+1 ); return k2; }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
  • 14. TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 ); // reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); // k1 is now k2's left subtree h = Max( height(k2->getRight()), k1->getHeight()); k2->setHeight( h+1 ); return k2; }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
  • 15. TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 ); // reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); // k1 is now k2's left subtree h = Max( height(k2->getRight()), k1->getHeight()); k2->setHeight( h+1 ); return k2; }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
  • 16. TreeNode<int>* singleLeftRotation( TreeNode<int>* k1 ) { if( k1 == NULL ) return NULL; // k2 is now the new root TreeNode<int>* k2 = k1->getRight(); k1->setRight( k2->getLeft() ); // Y k2->setLeft( k1 ); // reassign heights. First k1 (demoted) int h = Max(height(k1->getLeft()), height(k1->getRight())); k1->setHeight( h+1 ); // k1 is now k2's left subtree h = Max( height(k2->getRight()), k1->getHeight()); k2->setHeight( h+1 ); return k2; }  k 1 k 2 X Y Z k 1 k 2 X Y Z singleLeftRotation http://ecomputernotes.com
  • 17. TreeNode<int>* doubleRightLeftRotation(TreeNode<int>* k1) { if( k1 == NULL ) return NULL; // single right rotate with k3 (k1's right child) k1->setRight( singleRightRotation(k1->getRight())); // now single left rotate with k1 as the root return singleLeftRotation(k1); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
  • 18. TreeNode<int>* doubleRightLeftRotation(TreeNode<int>* k1) { if( k1 == NULL ) return NULL; // single right rotate with k3 (k1's right child) k1->setRight( singleRightRotation(k1->getRight())); // now single left rotate with k1 as the root return singleLeftRotation(k1); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
  • 19. TreeNode<int>* doubleRightLeftRotation(TreeNode<int>* k1) { if( k1 == NULL ) return NULL; // single right rotate with k3 (k1's right child) k1->setRight( singleRightRotation(k1->getRight())); // now single left rotate with k1 as the root return singleLeftRotation(k1); }  k 1 k 2 D A B C k 3 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
  • 20. TreeNode<int>* doubleLeftRightRotation(TreeNode<int>* k3) { if( k3 == NULL ) return NULL; // single left rotate with k1 (k3's left child) k3->setLeft( singleLeftRotation(k3->getLeft())); // now single right rotate with k3 as the root return singleRightRotation(k3); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
  • 21. TreeNode<int>* doubleLeftRightRotation(TreeNode<int>* k3) { if( k3 == NULL ) return NULL; // single left rotate with k1 (k3's left child) k3->setLeft( singleLeftRotation(k3->getLeft())); // now single right rotate with k3 as the root return singleRightRotation(k3); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
  • 22. TreeNode<int>* doubleLeftRightRotation(TreeNode<int>* k3) { if( k3 == NULL ) return NULL; // single left rotate with k1 (k3's left child) k3->setLeft( singleLeftRotation(k3->getLeft())); // now single right rotate with k3 as the root return singleRightRotation(k3); }  k 1 k 3 D A B C k 2 k 1 k 3 D A B C k 2 doubleRightLeftRotation http://ecomputernotes.com
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33. Deletion in AVL Tree Case 1a : the parent of the deleted node had a balance of 0 and the node was deleted in the parent’s left subtree. Action : change the balance of the parent node and stop. No further effect on balance of any higher node. Delete on this side http://ecomputernotes.com
  • 34. Deletion in AVL Tree Here is why; the height of left tree does not change. 1 2 3 4 5 6 7 0 1 2
  • 35. Deletion in AVL Tree Here is why; the height of left tree does not change. 1 2 3 4 5 6 7 2 3 4 5 6 7 0 1 2 remove(1)
  • 36. Deletion in AVL Tree Case 1b : the parent of the deleted node had a balance of 0 and the node was deleted in the parent’s right subtree. Action : (same as 1a ) change the balance of the parent node and stop. No further effect on balance of any higher node. Delete on this side
  • 37. Deletion in AVL Tree Case 2a : the parent of the deleted node had a balance of 1 and the node was deleted in the parent’s left subtree. Action : change the balance of the parent node. May have caused imbalance in higher nodes so continue up the tree. Delete on this side

Editor's Notes

  1. Start of lecture 23
  2. Start of lecture 24
  3. End of lecture 23.